//
//  ModifyPasswordViewController.swift
//  iModern
//
//  Created by Ralf on 8/17/15.
//  Copyright (c) 2015 com.starlooking. All rights reserved.
//

import UIKit
import XLForm

class ModifyPasswordViewController: FormViewController
{

    override func viewDidLoad()
    {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func viewWillAppear(animated: Bool)
    {
        self.title = "修改密码"
        initBackButton()
        self.initializeForm()
    }

    func initializeForm()
    {
        var form:    XLFormDescriptor
        var section: XLFormSectionDescriptor
        var row:     XLFormRowDescriptor

        form = XLFormDescriptor(title: "")

        section = XLFormSectionDescriptor.formSectionWithTitle("")
        form.addFormSection(section)

        row = XLFormRowDescriptor(tag: "oldPassword", rowType: XLFormRowDescriptorTypeText, title: "旧密码")
        row.cellConfigAtConfigure["textField.placeholder"] = "旧密码"
        row.required = true
        section.addFormRow(row)

        row = XLFormRowDescriptor(tag: "newPassword", rowType: XLFormRowDescriptorTypeText, title: "新密码")
        row.cellConfigAtConfigure["textField.placeholder"] = "新密码"
        row.required = true
        section.addFormRow(row)

        section = XLFormSectionDescriptor.formSectionWithTitle("")
        form.addFormSection(section)
        section = XLFormSectionDescriptor.formSectionWithTitle("")
        form.addFormSection(section)
        section = XLFormSectionDescriptor.formSectionWithTitle("")
        form.addFormSection(section)

        row = XLFormRowDescriptor(tag: "next", rowType: XLFormRowDescriptorTypeButton, title: "提  交")
        row.cellConfigAtConfigure["backgroundColor"] = Theme.theme_color
        row.cellConfig["textLabel.color"] = UIColor.whiteColor()
        row.action.formSelector = "submit:"
        row.required = true
        section.addFormRow(row)
        self.form = form
    }

    func submit(sender: XLFormRowDescriptor)
    {
        let cellphone = self.formValues()["oldPassword"] as? String
        if cellphone == nil
        {
            self.alertWarningMessage("旧密码不能为空")
            return
        }
        let newPassword = self.formValues()["newPassword"] as? String
        if newPassword == nil
        {
            self.alertWarningMessage("新密码不能为空")
            return
        }
        let oldPwd = self.formValues()["oldPassword"] as! String
        let newPwd = self.formValues()["newPassword"] as! String

        if self.validatePassword(newPwd)
        {
            let passWords = ["oldPassword": oldPwd, "newPassword": newPwd]
            let rs        = appDelegate.contactManager.updatePassword(passWords)
            if rs == nil
            {
                self.alertNetworkConnectionException()
            }

            if rs!.hasErrors()
            {
                if rs?.errorCode == 3020
                {
                    alertUserStatusException()
                    NSNotificationCenter.defaultCenter().postNotificationName("Logout", object: nil)
                    self.view.endEditing(true)
                    navigationController?.popToRootViewControllerAnimated(true)
                }
                alertErrorMessage(rs!.errorMessage)
                return
            }

            if rs.statusCode == 200
            {
                self.navigationController?.popViewControllerAnimated(true)
            }
        }
        else
        {
            self.alertErrorMessage("新密码长度应限制在4-16之间的字符和数字的组合")
        }
    }

    func validatePassword(passWord: String) -> Bool
    {
        let regex = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{4,16}$"
        let pred  = NSPredicate(format: "SELF MATCHES %@", regex)
        return pred.evaluateWithObject(passWord)
    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
